package view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JComponent;

import scheduler.DuplicateEventException;
import scheduler.Notifiable;
import scheduler.Scheduler;
import view.overlay.BorderLoc;
import view.overlay.Overlay;
import view.overlay.OverlayHost;

/**
 * 
 * @author matt
 *
 */
public class GameView extends JComponent {
	
	// GameView contains an OverlayHost so that it may add and remove overlays
	private OverlayHost mOverlayHost;
	
	public GameView() {
		mOverlayHost = new OverlayHost(this);
		
		this.setFocusable(true);
		
		
		
		this.addComponentListener(new ComponentListener() {

			@Override
			public void componentHidden(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void componentMoved(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void componentResized(ComponentEvent arg0) {
				mOverlayHost.resize(GameView.this.getWidth(), GameView.this.getHeight());
				System.out.format("GameView resized to: %dx%d\n", getWidth(), getHeight());
			}

			@Override
			public void componentShown(ComponentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		// Schedule the updater
		try {
			Scheduler.getPersistantClock().schedule(updater, 1);
		} catch (DuplicateEventException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	Notifiable updater = new Notifiable() {

		@Override
		public void execute() {
			GameView.this.repaint();
		}
		
	};
	
	/**
	 * Paints the component
	 */
	public void paint(Graphics g) {
//		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		Image cImage = mOverlayHost.getCompositeImage();
		
		if (cImage == null) return;
		
		g.drawImage(cImage, 0, 0, this);
	}
	
	
	
	public void addOverlay(Overlay overlay) {
		mOverlayHost.addOverlay(overlay, BorderLoc.CENTER);
	}
	
	public void addOverlay(Overlay overlay, BorderLoc loc) {
		mOverlayHost.addOverlay(overlay, loc);
	}
	
	public void removeOverlay(Overlay overlay) {
		
	}
	


}
